package com.ruoyi.business.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.JoBaseServiceImpl;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.order.domain.JbPaymentReceived;
import com.ruoyi.business.order.domain.vo.JbPaymentReceivedVO;
import com.ruoyi.business.order.mapper.JbPaymentReceivedMapper;
import com.ruoyi.business.order.service.JbPaymentReceivedService;
import com.ruoyi.business.sundry.service.SysDataScopeFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.StringJoiner;

/**
 * 回款Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class JbPaymentReceivedServiceImpl
        extends JoBaseServiceImpl<JbPaymentReceivedMapper, JbPaymentReceived>
        implements JbPaymentReceivedService
{

    @Autowired
    private SysDataScopeFactoryService sysDataScopeFactoryService;

    @Override
    public IPage<JbPaymentReceivedVO> pageList(PageParam pageParam, JbPaymentReceivedVO jbPaymentReceivedVO)
    {
        String ids = sysDataScopeFactoryService.fnStrDataScope(null);

        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT
                	jpr.*,\s
                	jo.process_number orderProcessNumber
                FROM
                	jb_payment_received jpr
                	left JOIN jb_order jo on jpr.related_order = jo.order_number
                	left JOIN sys_user su on jo.create_by = su.user_id\s
                WHERE
                	jpr.del_flag = 0\s
                """);

        sql.add(joSqlUtil.sqlIf(" and (jpr.payment_number like '%#{keyword}%' or jpr.customer_name like " +
                                "'%#{keyword}%' )", pageParam.getKeyword()));
        sql.add(joSqlUtil.sqlIf(" and jpr.payment_date <= '#{endTime}'", pageParam.getEndTime()));
        sql.add(joSqlUtil.sqlIf(" and jpr.payment_date >= '#{startTime}'", pageParam.getStartTime()));

        sql.add(joSqlUtil.sqlIf(" and jpr.related_order = '#{relatedOrder}'", jbPaymentReceivedVO.getRelatedOrder()));
        sql.add(joSqlUtil.sqlIf(" and jpr.status = '#{status}'", jbPaymentReceivedVO.getStatus()));
        sql.add(joSqlUtil.sqlIf(" and jpr.customer_id = '#{customerId}'", jbPaymentReceivedVO.getCustomerId()));
        sql.add(joSqlUtil.sqlIf(" and jpr.payment_method = '#{paymentMethod}'",
                jbPaymentReceivedVO.getPaymentMethod()));

        sql.add(joSqlUtil.sqlIf(" and jpr.submission_time <= '#{auditAddETime}'",
                jbPaymentReceivedVO.getAuditAddETime()));
        sql.add(joSqlUtil.sqlIf(" and jpr.submission_time >= '#{auditAddSTime}'",
                jbPaymentReceivedVO.getAuditAddSTime()));
        sql.add(joSqlUtil.sqlIf(" and jpr.approval_time <= '#{auditOverETime}'",
                jbPaymentReceivedVO.getAuditOverETime()));
        sql.add(joSqlUtil.sqlIf(" and jpr.approval_time >= '#{auditOverSTime}'",
                jbPaymentReceivedVO.getAuditOverSTime()));

        sql.add(joSqlUtil.sqlIf(StrUtil.format(" and jpr.create_by in ({})", ids), ids));
        sql.add(joSqlUtil.sqlIf(" and jo.create_by = '#{orderBy}'", jbPaymentReceivedVO.getOrderBy()));
        sql.add(joSqlUtil.sqlIf(" and su.dept_id = '#{orderByDept}'", jbPaymentReceivedVO.getOrderByDept()));

        sql.add(joSqlUtil.sqlIf("order by jpr.#{sortKey} " + (pageParam.isSort() ? "asc" : "desc"),
                pageParam.getSortKey()));

        String strSql = joSqlUtil.sqlJson(sql.toString(), pageParam);
        strSql = joSqlUtil.sqlJson(strSql, jbPaymentReceivedVO);

        IPage<JbPaymentReceivedVO> page = joSqlUtil.jsonToEntity(baseMapper.pageJson(pageParam.b(),
                strSql), JbPaymentReceivedVO.class);
        return page;
    }
}
